﻿using System.IO;

using System.Xml;
using Admin.Areas.Administration.Models;
using Admin.Models;
using System;
using System.Collections.Generic;
using System.Linq;

using System.Xml.Linq;

using System.Web;

namespace Admin.Areas.Administration.Common.MenuManager
{
    public class MenuManagement
    {
        public static List<TblMenuAdmin> ListMenuAdmin = new List<TblMenuAdmin>();

        public static List<TblMenuAdmin> MenuFromXml { get; set; }

        public static List<TblMenuAdmin> Load_menu()
        {
            BookingDBEntities db = new BookingDBEntities();
            
            try
            {
                List<TblMenuAdmin> listMenu = new List<TblMenuAdmin>();
                listMenu = db.TblMenuAdmins.ToList();

                var data = db.TblMenuAdmins.OrderBy(m => m.Display);
                return data.ToList();
                return null;
            }
            catch (Exception ex)
            {
                throw;
            }

           
        }

        public static List<TblMenuAdmin> LoadMenu()
        {
            TryLoadMenu(0, MenuFromXml);
            return ListMenuAdmin;
        }

        public static bool CheckMenuHasChild(int i)
        {
            if (MenuModel.MenuChildList(i).Count>0 && MenuModel.MenuChildList(i) !=null)
            {
                return true;
            }
            return false;
        }

        public static void TryLoadMenu(int i, List<TblMenuAdmin> list)
        {
            foreach (TblMenuAdmin item in list)
            {
                if (item.ParentId == i)
                {
                    ListMenuAdmin.Add(item);
                    if (CheckMenuHasChild(item.MenuId))
                    {
                        TryLoadMenu(Convert.ToInt32(item.MenuId), list);
                    }
                }
            }

           
        }

        public static void InitMenu() //Khoi tao menu
        {
            try
            {
                List<TblMenuAdmin> listMenuAdmin = new List<TblMenuAdmin>();
                listMenuAdmin = MenuManagement.Load_menu();
                string path = HttpContext.Current.Server.MapPath("~/App_Data/AdminMenu.xml");
                DirectoryInfo dirInfo = new DirectoryInfo(path);
                string fileName = dirInfo.FullName;

                if (!File.Exists(fileName))
                {
                    XmlDocument doc = new XmlDocument();
                    XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
                    doc.AppendChild(docNode);

                    XmlNode rootNode = doc.CreateElement("MenuAdmin");
                    doc.AppendChild(rootNode);

                    foreach (TblMenuAdmin item in listMenuAdmin)
                    {
                        XmlNode childNode = doc.CreateElement("MenuItem");// tao ra 1 dong

                        rootNode.AppendChild(childNode);// dua child node vao root node

                        XmlNode menuNode = doc.CreateElement("MenuID");
                        menuNode.AppendChild(doc.CreateTextNode(item.MenuId.ToString()));
                        childNode.AppendChild(menuNode);

                        XmlNode parentIdNode = doc.CreateElement("ParentID");
                        parentIdNode.AppendChild(doc.CreateTextNode(item.ParentId.ToString()));
                        childNode.AppendChild(parentIdNode);

                        XmlNode linkTextNode = doc.CreateElement("LinkText");
                        linkTextNode.AppendChild(doc.CreateTextNode(item.LinkText));
                        childNode.AppendChild(linkTextNode);

                        XmlNode actionNode = doc.CreateElement("ActionName");
                        actionNode.AppendChild(doc.CreateTextNode(item.ActionName));
                        childNode.AppendChild(actionNode);

                        XmlNode controllerNode = doc.CreateElement("ControllerName");
                        controllerNode.AppendChild(doc.CreateTextNode(item.ControllerName));
                        childNode.AppendChild(controllerNode);

                        XmlNode displayOrder = doc.CreateElement("Display");
                        displayOrder.AppendChild(doc.CreateTextNode(item.Display.ToString()));
                        childNode.AppendChild(displayOrder);
                    }
                    doc.Save(fileName);

                    XElement x = XElement.Load(fileName);
                    List<TblMenuAdmin> tlList = (from q in x.Elements("MenuItem")
                                                 select new TblMenuAdmin
                                                 {
                                                     MenuId = Convert.ToInt32(q.Element("MenuID").Value),
                                                     ParentId = Convert.ToInt32(q.Element("ParentID").Value),
                                                     LinkText = q.Element("LinkText").Value,
                                                     ActionName = q.Element("ActionName").Value,
                                                     ControllerName = q.Element("ControllerName").Value,
                                                     Display = Convert.ToInt16(q.Element("Display").Value)
                                                 }).ToList();

                    MenuFromXml = tlList;

                }
                else
                {
                    XElement x = XElement.Load(fileName);
                    List<TblMenuAdmin> tlList = (from q in x.Elements("MenuItem")
                                                 select new TblMenuAdmin
                                                 {
                                                     MenuId = Convert.ToInt32(q.Element("MenuID").Value),
                                                     ParentId = Convert.ToInt32(q.Element("ParentID").Value),
                                                     LinkText = q.Element("LinkText").Value,
                                                     ActionName = q.Element("ActionName").Value,
                                                     ControllerName = q.Element("ControllerName").Value,
                                                     Display = Convert.ToInt16(q.Element("Display").Value)
                                                 }).ToList();

                    MenuFromXml = tlList;
                }
            }
            catch (Exception ex)
            {

                throw;
            }
        }

        public static void UpdateMenu()
        {
            string path = HttpContext.Current.Server.MapPath("~/App_Data/AdminMenu.xml");
            var dirInfo = new DirectoryInfo(path);
            string fileName = dirInfo.FullName;

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
                InitMenu();
            }


        }
    }

}
